package com.haierCamera.customapplication.ui.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.RegisterRepo;
import com.haierCamera.customapplication.databinding.HzklActivityResetAccountBinding;
import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLResetAccountActivity extends BaseActivity {
    private HzklActivityResetAccountBinding binding;
    @Inject
    RegisterRepo registerRepo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_reset_account);
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initData();
    }

    private void initData() {
        binding.resetBtn.setOnClickListener(view -> setAccount());
    }

    private void setAccount() {
        String phone = binding.accPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        //startActivity(new Intent(HZKLResetAccountActivity.this,HZKLUserCheckPhoneActivity.class).putExtra("title","更换手机号"))
        sendCode(phone);
    }

    private void sendCode(String phone) {
        registerRepo.getVcode(phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    //Toast.makeText(HZKLResetAccountActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    startActivityForResult(new Intent(HZKLResetAccountActivity.this, HZKLUserCheckPhoneActivity.class)
                            .putExtra("title", "更换手机号").putExtra("phone", phone), 100);
                    break;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            finish();
        }
    }
}
